// pages/foodManage/foodManage.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menu: [],
    arrheight: [],
    inde: 0,
    scrollToView: '',
    rest: '',
    selectList:'',
    num:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var arrheight = []
    wx.request({
      url: 'http://www.maycan.net.cn/msmk/lands?restaurantid=25',
      success: function (res) {

        var goods = JSON.parse(res.data.result.restaurant.menu);
        var rest = res.data.result.restaurant;
        var num=0

        var selectFoods = wx.getStorageSync('selectList');

        if (!selectFoods){
            selectFoods=[]
          }else{
            selectFoods = JSON.parse(selectFoods)
          }
        console.log(selectFoods)
        //计算高度
        var heiht = 0
        arrheight.push(heiht)
        goods.forEach((foods) => {
          heiht += foods.spus.length * 89 + 41
          arrheight.push(heiht)
        })

        goods.forEach((good) => {
          good.spus.forEach((food) => {
            food.count = 0;
            selectFoods.forEach((cai) => {
              if (food.food_id == cai.food_id) {
                food.count = cai.count

              }
             
            })

          })
        })
 
        _this.setData({
          menu: goods,
          arrheight: arrheight,
          rest: rest,
         
        });
        wx.setNavigationBarTitle({
          title: res.data.result.restaurant.name,

        });
      }
    });

    wx.getSystemInfo({

      success: function (res) {
        _this.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight - 40
        });
       
      }

    });



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  toEdit() {
    wx.navigateTo({
      url: '../foodEdit/foodEdit',
    })
  },
  scroll(e) {
    // event.detail = { scrollLeft, scrollTop, scrollHeight, scrollWidth, deltaX, deltaY }
    var arr = this.data.arrheight
    var scrollTop = e.detail.scrollTop

    // console.log(scrollTop)

    // 计算滑动的高度测算滑动的位置

    for (let i = 0; i < arr.length; i++) {

      let height1 = arr[i]

      let height2 = arr[i + 1]

      if ((scrollTop >= height1 && scrollTop < height2)) {

        this.setData({
          inde: i
        })
        // console.log(i)
        return 0
      }
    }
  },
  changeScroll(e) {
    
    var _this = this

    this.setData({

      scrollToView: e.currentTarget.dataset.id

    })
    setTimeout(function () {
      _this.setData({
        inde: e.currentTarget.dataset.index
      })
    }, 500)
  },
  //计算数量
  control(e) {
    var fIndex = e.currentTarget.dataset.idx;
    var SIndex = e.currentTarget.dataset.son
    var fun = e.currentTarget.dataset.fun
    var menu = this.data.menu
    var selectList=[]
    var num =0
 


    if (fun==1){
      menu[fIndex].spus[SIndex].count++
    }else{
      menu[fIndex].spus[SIndex].count--
    }
   

    menu.forEach((foods)=>{
      foods.spus.forEach((food)=>{
        if (food.count){
          selectList.push(food)
          num += food.count
        }
      })
    })

    this.setData({
      menu: menu,
      selectList: selectList,
      num: num

    })
  
    wx.setStorageSync('selectList', JSON.stringify(selectList))
  }
 
})